//////////////////////////////////////////
//                                      //
// AddNewAgent(Edit)                    // Open new window
// GetAgent(DisplayName)                // Returns the selected AgentList
// EditAgent()                          // Open new window with the selected AgentList
// PopulateAgents()                     //
// ClearAgents()                        //
// CreateAgent(DisplayName,AgentType)   //
// UpdateButtons()                      //
// RemoveAgent()                        //
// RemoveAgentEntry(DisplayName)        //
//                                      //
//////////////////////////////////////////


function AddNewAgent(Edit) {

window.openDialog("chrome://Ultima/content/Ultima_Agent_Window.xul" , "Forum Agent" , "chrome,titlebar,toolbar,alwaysraised,minimizable,centerscreen,resizable",Edit).focus();

}


function GetAgent( DisplayName ) {

var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
var AgentList = prefService.getCharPref('extensions.Ultima.AgentList').split(' |||||||');

for(q=0; q<AgentList.length; q++) {
var entry = AgentList[q].split(' #######');
if (DisplayName == entry[0]) return entry;
}

return false;

}


function EditAgent() {

var Tree_Agent = document.getElementById('AgentTree');
var DisplayName = Tree_Agent.view.getCellText(Tree_Agent.currentIndex,Tree_Agent.columns.getNamedColumn('headDisplayName'));

AddNewAgent( GetAgent( DisplayName ) );

}


function PopulateAgents() {

var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
var AgentList = prefService.getCharPref('extensions.Ultima.AgentList').split(' |||||||');

var temp = "";

for (q=1; q<AgentList.length; q++) {
temp = AgentList[q].split(' #######');
CreateAgent(temp[0],temp[2]);
}

}


function ClearAgents() {

var Tree_Children_Agent = document.getElementById('Tree_Children_Agent');

if (Tree_Children_Agent) {

var Item = Tree_Children_Agent.getElementsByTagName('treeitem');

if (Item && Item.length > 0) for(q=Item.length-1; q>=0; q--) Tree_Children_Agent.removeChild(Item[q]);

}

}


function CreateAgent(DisplayName,AgentType) {

var ctype    = "";
var new_item = "";
var new_cell = "";
var new_rows = "";

if ( AgentType == "a" ) ctype = "Forum Agent";
if ( AgentType == "b" ) ctype = "Utopia Pimp";
if ( AgentType == "c" ) ctype = "StingerNET";

new_item = document.createElement('treeitem'); new_item.setAttribute('id',DisplayName);
new_cell = document.createElement('treecell'); new_cell.setAttribute('label',DisplayName);
new_rows = document.createElement('treerow' ); new_rows.appendChild(new_cell);
new_cell = document.createElement('treecell'); new_cell.setAttribute('label',ctype);

new_rows.appendChild(new_cell);
new_item.appendChild(new_rows);

var Tree_Children_Agent = document.getElementById('Tree_Children_Agent');
if (Tree_Children_Agent) Tree_Children_Agent.appendChild(new_item );

}


function UpdateButtons() {

var selected = 0;

var start = new Object();
var end = new Object();

var CurrentTree = document.getElementById("AgentTree");
var numRanges = CurrentTree.view.selection.getRangeCount();

for (var tt = 0; tt < numRanges && selected < 2; tt++) {
CurrentTree.view.selection.getRangeAt(tt,start,end);
for (var itemIndex = start.value; itemIndex <= end.value && selected < 2; itemIndex++) selected++;
}

if (selected == 1) {

document.getElementById( "Agent_Edit_Button"   ).disabled = false;
document.getElementById( "Agent_Remove_Button" ).disabled = false;	

} else {

document.getElementById( "Agent_Edit_Button"   ).disabled = true;
document.getElementById( "Agent_Remove_Button" ).disabled = true;

}

}


function RemoveAgent() {

var start = new Object();
var end = new Object();

var CurrentTree = document.getElementById("AgentTree");
var numRanges = CurrentTree.view.selection.getRangeCount();

for (var ww = numRanges - 1; ww >= 0; ww--) {

CurrentTree.view.selection.getRangeAt(ww,start,end);

for (var qq = end.value; qq >= start.value; qq--) {

item = CurrentTree.view.getItemAtIndex(qq);
item.parentNode.removeChild(item);

DisplayName  = item.firstChild.firstChild.getAttribute("label");
RemoveAgentEntry( DisplayName );

}

}

}


function RemoveAgentEntry( DisplayName ) {

var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
var AgentList = prefService.getCharPref('extensions.Ultima.AgentList').split(' |||||||');

for(q=0; q<AgentList.length; q++) {

var entry = AgentList[q].split(' #######');

if ( DisplayName == entry[0] ) {

AgentList.splice(q,1);
Temp_Entry = AgentList.join(' |||||||');
prefService.setCharPref( 'extensions.Ultima.AgentList' , Temp_Entry );

}

}

}
